# This script will create census variables for CohenCole project

infile = open("new_bg_censusdata2.csv","r")

outfile = open("new_censusvars.csv","w")
outfiler = open("new_censusvars_radius.csv","w")

header = infile.readline().rstrip("\n")
headerList = header.split(",")

fipsDict = {}
for line in infile:
	line = line.rstrip("\n")
	myList = line.split(",")
	
	newDict = {}
	for i in range( len(headerList) ):
		if ( i>0 ):
			newDict[ headerList[i] ] = int( myList[i] )
			
	fips = myList[0]
	fipsDict[fips] = newDict

infile.close()

(temp,rest) = header.split(",",1)
outfile.write( "lat,long," + rest + ",minfips\n" )
outfiler.write( "lat,long," + rest + ",minfips\n" )

infile = open("NewBlockGroupMatches.csv","r")

for line in infile:
	line = line.rstrip("\n")
	myList = line.split("\t")
	
	coords = myList.pop(0).strip()
	outfile.write( coords )
	outfiler.write( coords )
	
	# First, find the block group that is the minimum distance from the bureau record
	min_dist = float(5)
	min_fips = "NA"
	for block in myList:
		(thisFips,dist) = block.split(",")
		if ( float( dist ) < min_dist ):
			min_dist = float( dist )
			min_fips = thisFips
			
	if ( (min_dist>1) or (min_fips=="NA") ):    # No matches
		for i in range( len( headerList ) ):
			if ( i > 0 ):
				outfile.write( ",-1" )
				outfiler.write( ",-1" )
		outfile.write("\n")
		outfiler.write("\n")
	else:
		# output the minimum distance result first
		for i in range( len( headerList ) ):
			if ( i> 0 ):
				outfile.write("," + str( fipsDict[min_fips][headerList[i]] ) )
		if ( min_fips=="NA" ):
			min_fips = "-1"
		outfile.write( "," + min_fips + "\n")
		
		# Now output the radius result
		holdDict = {}
		for i in range( len( headerList ) ):
			if ( i>0 ):
				holdDict[ headerList[i] ] = 0
					
		for block in myList:
			(thisFips,dist) = block.split(",")
			
			if ( float( dist )<=1 ):
				for i in range( len( headerList ) ):
					if ( i>0 ):
						thisCode = headerList[i]
						if ( ( holdDict[ thisCode ] < 0 ) or (fipsDict[thisFips][thisCode]<0) ):
							holdDict[ thisCode ] = -1
						else:
							holdDict[ thisCode ] = holdDict[ thisCode ] + fipsDict[thisFips][thisCode]
		for i in range( len( headerList ) ):
			if ( i>0 ):
				outfiler.write( "," + str( holdDict[ headerList[i] ] ) )
		outfiler.write("," + min_fips + "\n")


outfile.close()
outfiler.close()
								